<template>
  <div>
    <div id="box" v-bind="{style:'height:'+divHeight+'px'}">
      <li v-for="item in list" ref="lis">
        <img v-lazy="item.albumpic_big">
        <!--<img :src="item.albumpic_big" alt="">-->
        <p>《{{item.songname}}》</p>
        <p>{{item.singername}}</p>
        <div class="smallBox" @click="setId(item.songid)">
          <img src="../../static/img/time1.png" alt="">
        </div>
      </li>
    </div>
  </div>
</template>

<script>
  export default {
    props: ['id'],//接收父元素传过来的参数
    data(){
      return {
        list: '',
        page: 1,
        divHeight:''
      }
    },
    created(){
      this.divHeight = (document.body.clientHeight) - 90 - 32 - 57;
    },
    watch: {
//      监听父元素传过来的值，如果有改变，重新调用ajax
      id: function () {
        this.getFrom(this.id);
      }
    },
    methods: {
      getFrom(){
        var url = 'http://route.showapi.com/213-1?showapi_appid=37413&showapi_sign=b7c3e87eb6354b7f93d93bf0859d6361&keyword=' + this.id + '&page=' + this.page;
        this.$http.get(url).then(function (response) {
          var obj = response.bodyText;
          this.list = JSON.parse(obj).showapi_res_body.pagebean.contentlist;

          if (this.list) {
            this.list.forEach(function (el) {
              if (!el.albumpic_big){
                el.albumpic_big = '../../static/img/timgs.jpg'
              }
              if (el.songname.length > 8) {
                el.songname = el.songname.slice(0, 8);
                el.songname = el.songname + '...';
              }
              if (el.singername.length > 8) {
                el.singername = el.singername.slice(0, 8);
                el.singername = el.singername + '...';
              }
            })
          }
          //this.divHeight = (document.body.clientHeight) - 90 - 32 - 57;
          console.log(this.list);
        })
      },
      setId(songid){
        console.log(songid);
        this.$emit('sind',songid);
      }
    }
  }
</script>

<style scoped>
  #box {
    /*overflow: hidden;*/
    overflow-y: auto;
    height: 300px;
  }

  ul {
    margin-top: 0;
    padding-left: 0;
  }

  li {
    float: left;
    width: 50%;
    list-style: none;
    text-align: center;
    position: relative;
  }

  img {
    display: block;
    width: 80%;
    margin: 5px auto;
    border: 1px solid rgba(0, 0, 0, .3);
    box-shadow: 3px 3px 3px rgba(0, 0, 0, .3);
  }

  .smallBox {
    height: 40px;
    width: 80%;
    background-color: rgba(0, 0, 0, .5);
    position: absolute;
    bottom: 67px;
    border-radius: 5px;
    left: 10%;
  }

  .smallBox img {
    position: absolute;
    right: 2%;
    bottom: 5%;
    width: 25px;
    height: 25px;
  }

  .box li > p {
    font-size: 12px;
    margin: 0;
  }
</style>
